package com.gm.wholesale.module.user;
import android.text.TextUtils;
import com.gm.commonlib.util.PrefsUtils.PrefUtils;
import com.gm.commonlib.util.jsonUtils.JsonUtils;
import com.gm.wholesale.module.user.bean.UserBean;
import com.gm.wholesale.module.user.bean.UserDetailBean;
import com.gm.wholesale.util.Utils;
public class UserDataManger {
    private static final String FILE_USER = "file_user";

    private static final String USER_INFO = "user_Info";

    private UserDataManger() {
    }

    private static UserDataManger mUserDataManger;
    private UserBean mUserBane;

    public  String getToken() {
        if (getUserInfo()!=null){
            return getUserInfo().getToken();
        }
      // return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiLpu4TmiJDnp5EiLCJ1c2VyX2lkIjo5NDksInVzZXJfdHlwZSI6MiwidGVuYW50Ijp7InRlbmFudF9pZCI6OTQ2LCJleHBpcmVfYXQiOjUxOTQ1OTA4MjR9LCJtb2R1bGVzIjpbMTAwMDAwLDExMDAwMCwxMjAwMDAsMTQwMDAwLDE1MDAwMCwxNjAwMDAsMTcwMDAwLDE4MDAwMCwxOTAwMDAsMjAwMDAwLDEwMDAwMSwxMTAzMDAsMTEwNDAwLDExMDUwMCwxMTAxMDEsMTIwMDAxLDEzMDAwMSwxMzAwMDIsMTUwMDAxLDE2MDMwMCwxNzAwMDEsMTMwMDAwLDE2MDEwMCwxNjA0MDAsMTEwMTAwLDE3MDAwMywxMjAxMDAsMTEwMjAwLDEyMDAwMiwxNzAwMDIsMTkwMTAwLDE0MDAwMSwxNjAyMDBdLCJhdWQiOlsi56ef5oi36LSm5Y-3Il0sImV4cCI6MTcxMTAwOTA5MCwiaWF0IjoxNzEwNDA0MjkwfQ.IzK2Sun5VpLIpXYRFndvD-9BUrstPqZA-6cyMZ6JQRE";
       return "";
    }

    public  String getUId() {
        if (getUserInfo()!=null){
            return getUserInfo().getUnionId();
        }
        return "";
    }

    public long getUserId(){
        UserDetailBean userDetailBean = UserDataManger.newInstance().getUserDetailBean();
        if (userDetailBean!=null){
            return userDetailBean.getUserId();
        }
        return 0;
    }

    public static UserDataManger newInstance() {
        if (mUserDataManger == null) {
            mUserDataManger = new UserDataManger();
        }
        return mUserDataManger;
    }
    public String getUserName(){
        if (getUserInfo()!=null){
            return getUserInfo().getName();
        }
        return "";
    }

    public String getUserPhone(){
        if (getUserInfo()!=null){
            return getUserInfo().getPhone();
        }
        return "";
    }

    //保存用户信息
    public void saveUserInfo(String userInfo) {
        setString(USER_INFO, userInfo);
    }

    public UserBean getUserInfo() {
        if (mUserBane != null) {
            return mUserBane;
        }
        String userInfo = getString(USER_INFO);
        if (TextUtils.isEmpty(userInfo)) {
            return null;
        }
        mUserBane = JsonUtils.parse(userInfo, UserBean.class);
        return mUserBane;
    }

    private void setString(String key, String value) {
        PrefUtils.setString(FILE_USER, key, value);
    }

    private String getString(String key) {
        return PrefUtils.getString(FILE_USER, key);
    }

    public UserDetailBean getUserDetailBean(){
        String token= Utils.getToken();
        if (TextUtils.isEmpty(token)){
            return null;
        }
        return JsonUtils.parse(token,UserDetailBean.class);
    }

   public void clear(){
       mUserBane=null;
       PrefUtils.clearDatas(FILE_USER);
       PrefUtils.clearDatas(USER_INFO);
       saveUserInfo(null);

   }
}
